package cat.joronya.supermanager.team;

import java.text.NumberFormat;
import java.util.Locale;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;
import cat.joronya.supermanager.R;
import cat.joronya.supermanager.Supermanager;

/**
 * Activity to list the user teams.
 * @author pol
 */
public class MyTeamsListActivity extends ListActivity 
{
	// pel Log
	private static final String TAG = "supermanager";
	
    // jornada que estem mostrant
    private int day = 0;
    private int nextDay = 0;
    
    protected Cursor cursor = null;
    
	/** 
	 * Called when the activity is first created. 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_teams_list);
    
		// amaguem el ProgressBar
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.my_teams_list_loading);
        progressBar.setVisibility(View.INVISIBLE);
        
        // If no data was given in the intent, then use our default content provider.
        Intent intent = getIntent();
        Uri plUri = getIntent().getData();
        if( plUri == null) {
        	plUri = Supermanager.MyTeam.CONTENT_URI;
            intent.setData(Supermanager.MyTeam.CONTENT_URI);
        }
	
        // retrieve day and status
        Cursor duetimeCursor = getContentResolver().query(Supermanager.Home.CONTENT_URI, 
        		Supermanager.Home.PROJECTION,
        		null, null, Supermanager.Home.DEFAULT_SORT_ORDER);
        if( duetimeCursor.moveToFirst() )
        {
        	day = duetimeCursor.getInt(1);
        	nextDay = duetimeCursor.getInt(5);
        }
        duetimeCursor.close();
        
        // Mostrem la jornada actual a la capçalera
        TextView dayTextVieww = (TextView) findViewById(R.id.my_teams_list_jornada);
        dayTextVieww.setText(getResources().getString(R.string.my_teams_list_jornada, new Integer(day).toString()));
        
        // tenim cursor?
		if( cursor != null )
			stopManagingCursor(cursor);

		MyTeamsProvider myTeamsProvider = new MyTeamsProvider();
		cursor = myTeamsProvider.getTeamList(this, ""+day, ""+nextDay);
        
        startManagingCursor(cursor);
        
		// Used to map notes entries from the database to views
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
        		R.layout.my_teams_list_row, 
        		cursor,
                new String[] { Supermanager.MyTeam.ID_EQU,
        					   Supermanager.MyTeam.NAME,
        					   Supermanager.MyTeam.POINTS,
        					   Supermanager.MyTeam.POINTS_CLASSIFICATION,
        					   Supermanager.MyTeam.GENERAL,
        					   Supermanager.MyTeam.GENERAL_CLASSIFICATION,
        					   Supermanager.MyTeam.BROKER,
        					   Supermanager.MyTeam.BROKER_CLASSIFICATION,
        					   Supermanager.MyTeam.STATUS,
        					   Supermanager.MyTeam.TEAM_PLAYERS}, 
        		new int[] { R.id.my_teams_list_row_id,
        					R.id.my_teams_list_row_name,
        					R.id.my_teams_list_row_jornada,
        					R.id.my_teams_list_row_jornada2,
        					R.id.my_teams_list_row_general,
        					R.id.my_teams_list_row_general2,
        					R.id.my_teams_list_row_broker,
        					R.id.my_teams_list_row_broker2,
        					R.id.my_teams_list_row_status,
        					R.id.my_teams_list_row_team_players});
        
        final NumberFormat nf = NumberFormat.getInstance(new Locale("es", "es"));
        
        adapter.setViewBinder(new ViewBinder()
        {
        	public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex)
        	{
        		// points
        		if( aColumnIndex == 4 )
        		{
        			float value = aCursor.getFloat(aColumnIndex);
        			TextView textView = (TextView) aView;
        			textView.setText(nf.format(value));
        			return true;
        		}
        		// general
        		if( aColumnIndex == 5 )
        		{
        			float value = aCursor.getFloat(aColumnIndex);
        			TextView textView = (TextView) aView;
        			textView.setText(nf.format(value));
        			return true;
        		}
        		// broker
        		if( aColumnIndex == 6 )
        		{
        			int value = aCursor.getInt(aColumnIndex);
        			TextView textView = (TextView) aView;
        			textView.setText(nf.format(value));
        			return true;
        		}
        		// classifications jornada
        		if( aColumnIndex == 7 )
        		{
        			int value = aCursor.getInt(aColumnIndex);
        			TextView textView = (TextView) aView;
        			textView.setText("("+nf.format(value)+"º)");
        			return true;
        		}
        		// classifications general
        		if( aColumnIndex == 8 )
        		{
        			int value = aCursor.getInt(aColumnIndex);
        			TextView textView = (TextView) aView;
        			textView.setText("("+nf.format(value)+"º)");
        			return true;
        		}
        		// classifications broker
        		if( aColumnIndex == 9 )
        		{
        			int value = aCursor.getInt(aColumnIndex);
        			TextView textView = (TextView) aView;
        			textView.setText("("+nf.format(value)+"º)");
        			return true;
        		}
        		// status
        		if( aColumnIndex == 14 )
        		{
        			int status = aCursor.getInt(aColumnIndex);
        			ImageView statusView = (ImageView)aView;
    				switch(status)
    				{
    					case Supermanager.MyTeam.STATUS_LESIONAT:
    	        			aView.setVisibility(View.VISIBLE);
    						statusView.setImageResource(R.drawable.lesio);
    						break;
    					case Supermanager.MyTeam.STATUS_DESCARTAT:
    	        			aView.setVisibility(View.VISIBLE);
    						statusView.setImageResource(R.drawable.baixa);
    						break;
    					case Supermanager.MyTeam.STATUS_INCOMPLETE_TEAM:
    	        			aView.setVisibility(View.VISIBLE);
    						statusView.setImageResource(R.drawable.incomplet);
    						break;
    					case Supermanager.MyTeam.STATUS_NORMAL:
    					default:
    						aView.setVisibility(View.INVISIBLE);
    						break;
    				}
    				statusView.bringToFront();
    				return true;
        		}
        		// team players
        		if( aColumnIndex == 15 )
        		{
        			String teamPlayers = aCursor.getString(aColumnIndex);
        			int status = aCursor.getInt(14);
        			Log.d(TAG,"MyTeamsList status: "+status+", teamPlayers: "+teamPlayers);
    				
        			if( status == Supermanager.MyTeam.STATUS_INCOMPLETE_TEAM )
        			{
        				TextView textView = (TextView) aView;
        				textView.setText(getResources().getString(R.string.my_teams_list_row_team_players, teamPlayers));
        				textView.bringToFront();
        				aView.setVisibility(View.VISIBLE);
        			}
        			else
        			{
        				aView.setVisibility(View.INVISIBLE);
        			}
        			return true;
        		}
        		return false;
        	}
        });
        
        setListAdapter(adapter);
	}
	
	/**
	 * Al clicar un team anem al detall.
	 */
	protected void onListItemClick (ListView l, View v, int position, long id)
	{
		int idequ = 0;
		try
		{
			TextView textView = (TextView)v.findViewById(R.id.my_teams_list_row_id);
			idequ = Integer.parseInt(textView.getText().toString());
		}catch(Exception e){}
		
		Log.d(TAG,"veient detall de team amb idequ: "+idequ);
		
		// mostrem l'activitat del detall de l'equip passant idequ
		startActivity(new Intent(this,MyTeamDetailActivity.class)
			.setData(Supermanager.MyTeamDetail.CONTENT_URI)
			.putExtra("cat.joronya.supermanager.myteams.idequ", idequ));
	}
	
}
